package com.wangzhixuan.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.wangzhixuan.commons.base.JdbcDao;
import com.wangzhixuan.commons.utils.MD5Util;


public class WebUtilService {
	@SuppressWarnings("rawtypes")
	public static List login(String username,String pwd){
		pwd = MD5Util.MD5(pwd);
		String sub_sql = "select * from T_AUTH_USERINFO where login_name = '"+username+"' and password = '"+pwd+"'";
		List list = JdbcDao.query(sub_sql);
		return list;
	}
	
	
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static List east(String param){
		
		String father_sql = "select name,TO_CHAR(GROUPMRID) groupid,to_char(substr(to_char(groupmrid),6)) mrid from enddevicepsrgroup t where t.groupmrid not in (select childgroupid from enddevicepsrgroupcomp) and (t.grouptype=3 or t.grouptype =4) and t.isvalid =1";
		List father_list = JdbcDao.query(father_sql);
		String sub_sql = "select name,TO_CHAR(GROUPMRID) groupid,to_char(substr(to_char(groupmrid),6)) mrid,a.fathergroupid fatherid from enddevicepsrgroup t,enddevicepsrgroupcomp a where t.isvalid=1  and t.groupmrid = a.childgroupid";
		List sub_list = JdbcDao.query(sub_sql);
		List result = getTreeData(father_list,sub_list);
		if(param!=null && !param.toString().trim().isEmpty()){
			String[] params = param.toString().split(",");
			String devaddr = "";
			for(String d : params){
				devaddr+= "'"+d+"',";
			}
			devaddr = devaddr.substring(0, devaddr.length()-1);
			String devaddr_sql = "select distinct commchannelname,devaddr from yh_view where devaddr in ("+devaddr+")";
			List devaddr_list = JdbcDao.query(devaddr_sql);
			List list = new ArrayList<>();
			Map map = new HashMap<>();
			map.put("id", "2");
			map.put("text", "终端列表");
			if(devaddr_list!=null && devaddr_list.size()>0){
				List l = new ArrayList<>();
				for(int i=0;i<devaddr_list.size();i++){
					Map dev_map = (Map) devaddr_list.get(i);
					dev_map.put("id", "0"+i);
					dev_map.put("text", dev_map.get("devaddr"));
					List s = new ArrayList<>();
					Map m = new HashMap<>();
					m.put("id", "click"+i+dev_map.get("devaddr"));
					m.put("text", dev_map.get("commchannelname"));
					m.put("devaddr", dev_map.get("devaddr"));
					s.add(m);
					dev_map.put("children",s );
					l.add(dev_map);
				}
				map.put("children", l);
			}
	
			Map m = new HashMap<>();
			m.put("id", "0");
			m.put("text", "对象列表");
			List res = new ArrayList<>();
			res.add(map);
			res.add(result.get(0));
			m.put("children", res);
			m.put( "state","open");
			list.add(m);
			return list;
		}
		return result;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private static List getTreeData(List father_list, List sub_list){
		List father = new ArrayList<>();
		List list = new ArrayList<>();
		Map m = new HashMap<>();
		m.put("id", "1");
		m.put("text", "群组列表");
		
		for(int i=0;i<father_list.size();i++){
			Map f = (Map) father_list.get(i);
			Map map = new HashMap<>();
			map.put("id", ""+i);
			map.put("text", f.get("name"));
			
			String groupid = f.get("groupid").toString();
			
			List sub = getTreeDataSub(groupid,sub_list,i);
			if(sub != null && sub.size()>0){
				map.put("children", sub);
				map.put( "state","closed");
			}
			list.add(map);
		}
		m.put("children", list);
		father.add(m);
		m.put( "state","closed");
		return father;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private static List getTreeDataSub(String groupid, List sub_list, int index){
		List list = new ArrayList<>();
		for(int i=0;i<sub_list.size();i++){
			Map map = (Map) sub_list.get(i);
			if(map.get("fatherid").toString().equals(groupid)){
				map.put("id", "1"+index+i);
				map.put("text", map.get("name"));
				
				String id = map.get("groupid").toString();
				List sub = getTreeDataSub(id,sub_list,i);
				
				if(sub != null && sub.size()>0){
					map.put("children", sub);
					map.put( "state","closed");
				}
				list.add(map);
			}
		}
		return list;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static Object west(String devaddr) {
		String sql = "select distinct customername,devaddr,servicesuppliername,companyname,devtype,commchannelname,modeomspeed,devstatus from yh_view where devaddr = "+ devaddr;
		List list = JdbcDao.query(sql);
		Map result  = (Map) list.get(0);
		Map map = new HashMap<>();
		map.put("total", "8");
		String[] keys = {"customername","devaddr","servicesuppliername","companyname","devtype","commchannelname","modeomspeed","devstatus"};
		String[] titles = {"户名","终端地址码","供电单位","终端制造商","终端型号","所属信道","速率","状态"};
		List l = new ArrayList<>();
		for(int i=0;i<keys.length;i++){
			Map m = new HashMap<>();
			m.put("value", result.get(keys[i]));
			m.put("name", titles[i]);
			l.add(m);
		}
		map.put("rows", l);
		return map;
	}
}
